import {
  ADD_NUM,
  CHANGE_BANNERS,
  CHANGE_RECOMMENDS,
  SUB_NUM,
} from './constants'
import axios from 'axios'

export const addNumberAction = (num) => ({
  type: ADD_NUM,
  num,
})

export const subNumberAction = (num) => ({
  type: SUB_NUM,
  num,
})

export const changeBanners = (banners) => ({
  type: CHANGE_BANNERS,
  banners,
})

export const changeRecommends = (recommends) => ({
  type: CHANGE_RECOMMENDS,
  recommends,
})

export const fetchHomeMultidataAction = () => {
  // 如果是一个普通的action，那么我们这里需要汾酒action对象
  // 问题 对象中时不能直接拿到从服务器请求的异步数据的
  // return {}

  return function (dispatch, getState) {
    axios.get('http://123.207.32.32:8000/home/multidata').then((res) => {
      const banners = res.data.data.banner.list
      const recommends = res.data.data.recommend.list
      dispatch(changeBanners(banners))
      dispatch(changeRecommends(recommends))
    })
  }

  // 如果返回的是一个函数，那么redux是不支持的
  // 正常情况下store.dispatch(object)，想要派发函数store.dispatch(function)增强
}
